package com.thinkgem.jeesite.modules.devmng.web;

import com.thinkgem.jeesite.common.annotation.MobileApi;
import com.thinkgem.jeesite.common.exception.MobileApiException;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseMessage;
import com.thinkgem.jeesite.common.web.BaseMobileApiController;
import com.thinkgem.jeesite.modules.devmng.vo.AppUserModifyPasswordRequester;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.MobileUserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

//低耦合?
//不需要Mobileservice
//格式化
@MobileApi
@RestController
@RequestMapping("/mobile-api/user")
public class UserMobileController extends BaseMobileApiController {

    @Autowired
    private SystemService systemService;
    @RequestMapping(value = "/modifyPassword", method = RequestMethod.POST)
    public BaseMessage modifyPassword(@RequestBody AppUserModifyPasswordRequester appUserModifyPasswordRequester) {
        User user = MobileUserContext.get();
        String oldPassword = appUserModifyPasswordRequester.getOldPassword();
        String newPassword = appUserModifyPasswordRequester.getNewPassword();
        String verifyPassword = appUserModifyPasswordRequester.getVerifyPassword();
        boolean flag = StringUtils.isNotBlank(oldPassword)
                && StringUtils.isNotBlank(newPassword)
                && StringUtils.isNotBlank(verifyPassword);
        if (!(flag)) {
            throw new MobileApiException("密码不为空");
        }

        if (!SystemService.validatePassword(oldPassword, user.getPassword())) {
            throw new MobileApiException("密码不正确");
        }

        if (!newPassword.equals(verifyPassword)) {
            throw new MobileApiException("新密码验证密码不一致");
        }
        systemService.updatePasswordById(user.getId(), user.getLoginName(), newPassword);
        return BaseMessage.getDefaultSuccessMessage();

    }
}
